import { Node } from 'cc';
import { ecs } from 'db://app-game/libs/ecs/ECS';
import { Circle } from 'db://app-game/libs/math/auad-tree/Circle';
import QuadTreeUtil from 'db://app-game/libs/math/auad-tree/QuadTreeUtil';
import { Rectangle } from 'db://app-game/libs/math/auad-tree/Rectangle';

/** 圆形监控范围 */
@ecs.register('CircleModelMonitor')
export class CircleModelMonitorComp extends ecs.Comp {
  /** 自身节点 */
  node: Node = null!;
  /** 监视节点名称 */
  monitorNodeName: string = '';
  /** 监视半径 */
  monitorRange: { xr: number; yr: number } = { xr: 0, yr: 0 };
  /** 碰撞列表函数 */
  getColliderList: (params: {
    name: string;
    circle: Circle<any>;
  }) => (Circle<any> | Rectangle<any>)[] = null!;

  /** 碰撞列表 */
  get colliderList() {
    // 1.创建圆形
    const circle = new Circle({
      x: this.node.position.x,
      y: this.node.position.y,
      xr: this.monitorRange.xr,
      yr: this.monitorRange.yr,
      data: { node: this.node },
    });

    // 2.创建碰撞列表函数
    const colliderParams = {
      name: this.monitorNodeName,
      circle,
    };

    // 3.获取碰撞列表
    let colliders = this.getColliderList(colliderParams);
    return QuadTreeUtil.getColliderList(circle, colliders);
  }

  reset(): void {
    this.node = null!;
    this.monitorNodeName = '';
    this.monitorRange = { xr: 0, yr: 0 };
    this.getColliderList = null!;
  }
}
